using System.Collections.Generic;
using System.Timers;

public abstract class LogicController
{
	protected string startLogicName = "Start";

	public string cLogiName = string.Empty;

	protected Logic cLogic;

	protected Dictionary<string, Logic> logics = new Dictionary<string, Logic>();

	protected List<ILogicAssigner> assigners = new List<ILogicAssigner>();

	private Timer timer;

	public LogicController()
	{
		Init();
		Start();
	}

	protected abstract void Init();

	public virtual void AddAssigner(ILogicAssigner assigner)
	{
		if (!assigners.Contains(assigner))
		{
			assigners.Add(assigner);
		}
		else
		{
			BCWDebug.LogError("Assigner already activated " + assigner.ToString());
		}
	}

	public virtual void RemoveAssigner(ILogicAssigner assigner)
	{
		assigners.Remove(assigner);
	}

	public virtual void ChangeLogic(string logicName, params object[] args)
	{
		if (logics.ContainsKey(logicName))
		{
			if (cLogic != null)
			{
				cLogic.OnStop();
			}
			cLogic = logics[logicName];
			cLogic.OnStart(this, cLogiName, args);
			cLogiName = logicName;
		}
	}

	public virtual void OnLogicEvent(int name, params object[] args)
	{
		CoroutineRunner.CallInMainThread(delegate
		{
			for (int i = 0; i < assigners.Count; i++)
			{
				if (assigners[i] != null)
				{
					assigners[i].OnLogicEvent(name, args);
				}
				else
				{
					assigners.RemoveAt(i);
					i--;
				}
			}
		});
	}

	public virtual void OnAssignerEvent(int name, params object[] args)
	{
		cLogic.OnEvent(name, args);
	}

	protected virtual void Start()
	{
		if (logics.ContainsKey(startLogicName))
		{
			ChangeLogic(startLogicName);
		}
		else
		{
			BCWDebug.LogError("Logic " + startLogicName + " not exist");
		}
	}
}
